package com.mlethe.android.demo.recyclerview.adapter;


import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mlethe.demo.recyclerview.adapter.R;
import com.mlethe.library.recyclerview.decoration.PagerGridItemDecoration;
import com.mlethe.library.recyclerview.pagergrid.PagerGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ViewPagerGridAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        recyclerView = findViewById(R.id.recyclerView);
        int row = 3;
        int column = 2;
        PagerGridLayoutManager manager = new PagerGridLayoutManager(row, column, PagerGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new ViewPagerGridAdapter();
//        adapter.setColumn(column).setRow(row);
        adapter.setOnItemClickListener((v, item, position) -> {
            Toast.makeText(this, "item->" + position, Toast.LENGTH_SHORT).show();
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new PagerGridItemDecoration(this).setHorizontalSize(10f).setVerticalSize(14f).setDrawable(R.mipmap.ic_stcs_btm_line));
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.setNewData(getData(13, 1));
            }
        }, 100);
    }

    public List<String> getData(int index, int type) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < index; i++) {
            if (type == 1) {
                list.add("刷新" + (i + 1));
            } else if (type == 2){
                list.add("重新加载" + (i + 1));
            } else {
                list.add("加载" + (i + 1));
            }
        }
        return list;
    }

    /**
     * 获取屏幕宽度
     *
     * @return
     */
    private int getWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    /**
     * dp转px
     *
     * @param context
     * @param dp
     * @return
     */
    private float dp2px(Context context, float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }
}
